// slurm_test.c
// 用于测试 Slurm 的 OpenMP 多线程调度负载程序

#include <stdio.h>
#include <stdlib.h>
#include <omp.h>
#include <time.h>

#define SIZE 1024
#define ITER 250

void heavy_compute() {
    double **a = malloc(SIZE * sizeof(double*));
    double **b = malloc(SIZE * sizeof(double*));
    double **c = malloc(SIZE * sizeof(double*));

    for (int i = 0; i < SIZE; ++i) {
        a[i] = malloc(SIZE * sizeof(double));
        b[i] = malloc(SIZE * sizeof(double));
        c[i] = malloc(SIZE * sizeof(double));
        for (int j = 0; j < SIZE; ++j) {
            a[i][j] = (double)(i + j);
            b[i][j] = (double)(i - j);
            c[i][j] = 0.0;
        }
    }

    for (int k = 0; k < ITER; ++k) {
        #pragma omp parallel for
        for (int i = 0; i < SIZE; ++i) {
            for (int j = 0; j < SIZE; ++j) {
                double sum = 0.0;
                for (int l = 0; l < SIZE; ++l) {
                    sum += a[i][l] * b[l][j];
                }
                c[i][j] += sum;
            }
        }
    }

    printf("[INFO] Computation done.\n");

    for (int i = 0; i < SIZE; ++i) {
        free(a[i]);
        free(b[i]);
        free(c[i]);
    }
    free(a); free(b); free(c);
}

int main() {
    double start = omp_get_wtime();
    heavy_compute();
    double end = omp_get_wtime();
    printf("Total time: %.2f seconds\n", end - start);
    return 0;
}

